package cn.tedu.egarticle0618.controller;

import cn.tedu.egarticle0618.base.response.JsonResult;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpSession;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;


@Slf4j
@RestController
@Tag(name = "02-上传图片模块")
@RequestMapping("v1/upload/")
public class UploadController {
    @PostMapping("upload")
    @Operation(summary = "上传图片功能")
    @ApiOperationSupport(order=40)
    public JsonResult upload(MultipartFile file) throws IOException {
        String fileName = file.getOriginalFilename();
        String suffix = fileName.substring(fileName.lastIndexOf("."));
        UUID uuid = UUID.randomUUID();
        fileName = uuid + suffix;
        String dirPath = "D:/files/";
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
        String datePath = sdf.format(new Date());
        File file1 = new File(dirPath + datePath);
        if (!file1.exists()){
            file1.mkdirs();
        }
        String filePath = dirPath + datePath + fileName;
        file.transferTo(new File(filePath));
        return JsonResult.ok(datePath + fileName);
    }
}
